﻿using System;
using System.Windows.Input;

namespace Colorful.Control.Controls
{
    public sealed class DelegateCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private readonly SimpleEventHandler handler;
        private bool isEnabled;

        public DelegateCommand(SimpleEventHandler eventHandler)
        {
            isEnabled = true;
            handler = eventHandler;
        }

        private void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, EventArgs.Empty);
        }

        public bool CanExecute(object arg)
        {
            return IsEnabled;
        }

        public void Execute(object arg)
        {
            handler();
        }

        public bool IsEnabled
        {
            get
            {
                return isEnabled;
            }
            set
            {
                isEnabled = value;
                OnCanExecuteChanged();
            }
        }
    }
}
